<?php
class Model {
	public $pdo;
	public $table;
	public function __construct($table=null){
		$this->pdo = ConnexionDataBase::getInstance();
		if($table)
			$this->table = $table;
	}
	
	//insert un utilisateur dans la base
	public function insertUser($user){
		$req = $this->pdo->prepare('INSERT INTO utilisateur SET email_utilisateur = :email,
				pass_utilisateur = :pass, nom_utilisateur = :nom, prenom_utilisateur = :prenom, type_utilisateur = :type');
			$req->bindValue(':email',$user->getEmail());
			$req->bindValue(':pass',sha1($user->getPass()));
			$req->bindValue(':nom',$user->getNom());
			$req->bindValue(':prenom',$user->getPrenom());
			$req->bindValue(':type',$user->getType());
			$donnee = $req->execute();
			$req->closeCursor();
			return $donnee;
	}
	
	//permet de connecter un user au site
	public function connexion($email,$password){
		$req = $this->pdo->prepare('SELECT * FROM utilisateur WHERE email_utilisateur= :email AND 
		pass_utilisateur = :password ');
		$req->bindValue('email',$email);
		$req->bindValue('password',$password);
		$req->execute();
		$donnee = $req->fetch();
		return $donnee;
	}
	
	public function getId($email){
			$req = $this->pdo->prepare('SELECT id_utilisateur FROM utilisateur WHERE email_utilisateur = :email');
			$req->bindValue(':email',$email);
			$req->execute();
			$donnee = $req->fetch();
			return $donnee['id_utilisateur'];	
	}
	
	public function getUtilisateur($email,$password){
		
		$this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
		$req = $this->pdo->prepare('SELECT * FROM utilisateur WHERE email_utilisateur = :email AND pass_utilisateur
		= :password');
		$req->bindValue(':email',$email);
		$req->bindValue(':password',$password);
		$req->execute();
		$user = $req->fetch(PDO::FETCH_OBJ);
		return $user;
	}
	
	public function getAdresse($id_user){
		$this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
		$req = $this->pdo->prepare('SELECT * FROM adresse WHERE referent = :id'); 
		$req->bindValue(':id',$id_user,PDO::PARAM_INT);
		$req->execute();
		$adresse = $req->fetch(PDO::FETCH_OBJ);
		return $adresse;
	}
	
	public function getHotel($referent_hotel){
		$this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
		$req = $this->pdo->prepare('SELECT * FROM hotel WHERE referent_hotel = :referent_hotel ');
		$req->bindValue(':referent_hotel',$referent_hotel);
		$req->execute();
		$hotel = $req->fetch(PDO::FETCH_OBJ);
		return $hotel;
	}
	
	public function getDateTime(){
		$date_ajout = date('Y-m-d').' '.date('H:i:s');
		return $date_ajout;	
	}
}